from sqlalchemy.orm import Session
from models.user import User
from schemas.user import UserSearch
from schemas.response import ServerResponseEntity
from schemas.page import Page

def get_user_page(db:Session,us:UserSearch):
    query = db.query(User)

    if us.nickName:
        query = query.filter(User.nickName.like(f"%{us.nickName}%"))

    if us.status:
        query = query.filter(User.status == us.status)

    #分页处理
    total = query.count()
    users = query.offset((us.page.current-1)*us.page.size).limit(us.page.size).all()

    #分页数据
    p = Page()
    p.total = total
    p.records = users
    p.current = us.page.current
    p.size = us.page.size

    return ServerResponseEntity.success(p)